<?php

/**
 * This is the model class for table "{{user_payment}}".
 *
 * The followings are the available columns in table '{{user_payment}}':
 * @property integer $ID
 * @property integer $userID
 * @property double $money
 * @property integer $status
 * @property string $description
 * @property integer $datetime
 */
class UserPayment extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return UserPayment the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{user_payment}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('userID, money, status, description, datetime', 'required'),
			array('userID, status, datetime', 'numerical', 'integerOnly'=>true),
			array('money', 'numerical'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('ID, userID, money, status, description, datetime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'ID' => 'ID',
			'userID' => 'Người dùng',
			'money' => 'Số tiền',
			'status' => 'Trạng thái',
			'description' => 'Chú thích',
			'datetime' => 'Ngày giờ',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		$criteria->together = true;
		$criteria->join = 'LEFT JOIN tbl_user_profile p ON t.userID = p.userID';
		$criteria->addCondition('t.userID='.Yii::app()->user->id);
		if(!isset($_GET['UserPayment_sort'])) {
			$criteria->order = 't.datetime DESC';
		}

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array(
        'attributes'=>array(
            'userID'=>array(
                'asc'=>'p.fullname',
                'desc'=>'p.fullname DESC',
            ),
            '*',
        ),
   		),
			 'pagination'=>array(
        	'pageSize'=> Yii::app()->user->getState('pageSize',10),
    	),
		));
	}
}